**** this do-file 
*C) computes main Panama exposure measure at the country-pair level and several variants thereof used in robustness checks



clear all
global main "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository"

cd "$main"

global data "$main/data_intermediate"
global output "$data/output514"
global input "$data/input514"






u "$data/hrs_12_small", clear

keep if post ==0

* merge DWT on Neo-Panamax

merge m:1 A_aci D_ac using "$data/teuNEOP_small"
drop if _merge==2
drop _merge


collapse (sum) XP teu* (mean) tt (max) hop, by(DEP ARR n)
replace XP=0 if XP==2 /*few cases*/
replace XP=1 if XP==3/*few cases*/
drop n

collapse (mean) XP tt hop teu*, by(DEP ARR)

rename ARR A_acid 
rename DEP D_acid

g steuNEOP=teuNEOP/teu
g steuNEOP_pre=teuNEOP_pre/teu_pre
drop teu*

merge 1:1 *acid using "$data/port_to_port_sample_small", keepusing(*country *name)
keep if _merge==3
drop _merge 


save "$data/XPanama_byHrPRE514", replace




** EXPOSURE MEASURE AT THE COUNTRY-PAIR LEVEL


u "$data/xj2_small", clear
rename A* D*
rename x_j OUT_D
cross using "$data/xj2_small"
rename x_j OUT_A

drop OUT_A
merge m:1 A_acid using "$data/IN_A_acid_small"
drop _merge
rename dwt_usage OUT_A /*this is actually IN, but to preserve the code...*/

* merge new exposure
rename D_acid DEP
rename A_acid ARR

merge 1:1 DEP ARR using "$data/tt_NP_pre"
keep if _merge==3
drop _merge
* the merge==1 observation are routes to/from ports that have no trips in the pre-period

rename DEP D_acid 
rename ARR A_acid


merge 1:1 *acid using "$data/XPanama_byHrPRE514"
keep if _merge==3
drop _merge
* the merge==1 observation are routes to/from ports that have no trips in the pre-period

* merge country codes

merge 1:1 *acid using "$data/port_to_port_sample_small", keepusing(*country)
keep if _merge==3
drop _merge 


rename D_country iso2

merge m:1 iso2 using "data_input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge
replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"

rename iso3 iso_i
drop iso2

rename A_c iso2
merge m:1 iso2 using "data_input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge

replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"

rename iso3 iso_j
drop iso2

* weighted average across departure ports by exporting country and arrival port

foreach var of varlist steu* XP tt hop mDtt_XP {
replace `var'=`var'*OUT_D
}

collapse (sum) XP tt hop steu* OUT_D mDtt_XP, by(iso_i A_* OUT_A iso_j)

foreach var of varlist steu* tt hop XP mDtt_XP {
replace `var'=`var'/OUT_D*OUT_A
}

* gen weighted average across ports of the importing country

collapse (sum) XP tt hop steu* OUT_A mDtt_XP, by(iso_j iso_i)

foreach var of varlist steu* XP tt hop mDtt_XP {
replace `var'=`var'/OUT_A
}

drop OUT_A


save "$data/PESPbyHr_bil514", replace




/*

u "$data/XPanama_byHrPRE514", clear
drop steu*
rename XP aXP
rename tt att
rename hop ahop
merge 1:1 D_acid A_acid using "/Users/inga/Dropbox/Ais project/inga/data/XPanama_byHrPRE514" 

order aXP XP

*/



************************************************************************************************************************
**********               make Panama exposure measure for us district level               ******************************
************************************************************************************************************************


/*
u "$data/xj2_small", clear
rename A* D*
rename x_j OUT_D
cross using "$data/xj2_small"
rename x_j OUT_A

drop OUT_A
merge m:1 A_acid using "$data/IN_A_acid_small"
drop _merge
rename dwt_usage OUT_A /*this is actually IN, but to preserve the code...*/

* merge exposure

*merge 1:1 *acid using "$data/PanExposureSP2s_teu"
merge 1:m *acid using "$data/XPanama_byHrPRE514"
keep if _merge==3
drop _merge
replace XP=0 if XP==.


* gen weighted average across exporters ports

rename D_country iso2

merge m:1 iso2 using "$data/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge
replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"

rename iso3 iso_i
rename iso2 D_country

rename A_c iso2
merge m:1 iso2 using "$data/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge

replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"

rename iso3 iso_j
rename iso2 A_country


** merge US district code

merge m:1 A_country A_port_name using $data/port_list_sample2_wUScode, keepusing(US) /*dont merge on A_acid !!!*/
drop if _merge==2
drop _merge

tostring USdi, replace

replace iso_j = USdi if iso_j=="USA"
drop USdi

rename A_ac AA
rename D_ac A_acid

merge m:1 A_country A_port_name using $data/port_list_sample2_wUScode, keepusing(US)
drop if _merge==2
drop _merge
tostring USdi, replace
replace iso_i = USdi if iso_i=="USA"
drop USdi


foreach var of varlist *teu* XP {
replace `var'=`var'*OUT_D
}


collapse (sum) XP *teu* OUT_D, by(iso_i A_* OUT_A iso_j)

foreach var of varlist *teu* XP {
replace `var'=`var'/OUT_D*OUT_A
}

* gen weighted average across importers ports


collapse (sum) XP OUT_A *teu*, by(iso_j iso_i)

foreach var of varlist *teu*  XP {
replace `var'=`var'/OUT_A
}

drop OUT_A
save $data/PESP_byHr514us, replace



*/













************* instead of the most frequent route, compute a weighted avg *********





local files: dir "$soutput" files "*.dta"


foreach file of loc files {

*local file "hrs11_12"
u "$soutput/`file'", clear 
	
rename D_a D15_acid
rename A_a D16_acid	

drop if D0_a == D16_a

sort D16_a A_t D0_t
local l=1
while `l'>0 {
bysort D16_a: drop if D0_time[_n]<D0_time[_n-1] & D0_time[_n-1]!=.
local l = r(N_drop)
}


* keep only pre-route

keep if A_t<184*24

if _N>0 {

sort D0_a D16_a D0_t
bysort D0_a D16_ac: g n=_n

g DEP=D0_a
g ARR=D16_a

reshape long D@_acid, i(DEP ARR n) j(hop)

** drop superfluous hops

sort DEP ARR n hop
bysort DEP ARR n: drop if D_acid==D_acid[_n-1] & D_acid[_n-1]!=.

bysort DEP ARR n: g A_acid = D_acid[_n+1]

drop if A_acid==.

merge m:1 D_a A_ac using "$data/XPanama_ids_small"
drop if _merge==2
drop _merge

g tt=A_t-D0_t

merge m:1 A_aci D_ac using "$data/teuNEOP_small"
drop if _merge==2
drop _merge


collapse (sum) XP teu* (mean) tt (max) hops=hop, by(ARR DEP n D0_t)
replace XP=0 if XP==2
replace XP=0 if XP==4
replace XP=1 if XP==3

** generate weight

egen mxhr = max(D0_t), by(ARR DEP)

sort DEP ARR n
bysort DEP ARR: g per=D0_time-D0_time[_n-1]
replace per=D0_time if n==1

g weight=per/mxhr

egen test=total(weig), by(ARR DEP)
sum test
drop test


g steuNEOP=teuNEOP/teu
g steuNEOP_pre=teuNEOP_pre/teu_pre
drop teu*

* gen weighted average

foreach var of varlist steu* hops tt XP {
g wgt_`var'=weight*`var'
}

collapse (sum) wgt* (mean) steu* hops tt XP , by(ARR DEP)

rename ARR A_acid 
rename DEP D_acid

save "$output/wav`file'", replace
}

}


u "$output/wavhrs1_12", clear
forvalues i=2(1)514 {
capture noisily append using "$output/wavhrs`i'_12"
capture noisily erase "$output/wavhrs`i'_12.dta"
}
erase "$output/wavhrs1_12.dta"
* two missing: have no departures in the pre period

* merge country codes
merge 1:1 *acid using "$data/port_to_port_sample_small", keepusing(*country *name)
keep if _merge==3
drop _merge 
save "$data/XPanama_byHrPRE514wav", replace









** EXPOSURE MEASURE AT THE COUNTRY-PAIR LEVEL


u "$data/xj2_small", clear
rename A* D*
rename x_j OUT_D
cross using "$data/xj2_small"
rename x_j OUT_A

drop OUT_A
merge m:1 A_acid using "$data/IN_A_acid_small"
drop _merge
rename dwt_usage OUT_A /*this is actually IN, but to preserve the code...*/

* merge new exposure

merge 1:1 *acid using "$data/XPanama_byHrPRE514wav"
keep if _merge==3
drop _merge
rename XP wavXP
rename steuNEOP_pre wav_steuNEOP_pre
drop wgt_tt wgt_hops hops tt

* the merge==1 observation are routes to/from ports that have no trips in the pre-period


merge 1:1 *acid using "$data/maxC"
keep if _merge==3
drop _merge

rename D_country iso2

merge m:1 iso2 using "data_input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge
replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"

rename iso3 iso_i
drop iso2

rename A_c iso2
merge m:1 iso2 using "data_input/iso2toiso3_uqe", keepusing(iso3)
drop if _merge==2
drop _merge

replace iso3="TWN" if iso2=="TW"
replace iso3="PRI" if iso2=="PR"

rename iso3 iso_j
drop iso2

* weighted average across departure ports by exporting country and arrival port

foreach var of varlist steu* XP wav* wgt*{
replace `var'=`var'*OUT_D
}

collapse (sum) XP steu* OUT_D wav* wgt*, by(iso_i A_* OUT_A iso_j)

foreach var of varlist steu*  XP wav* wgt* {
replace `var'=`var'/OUT_D*OUT_A
}

* gen weighted average across ports of the importing country

collapse (sum) XP steu* OUT_A wav* wgt*, by(iso_j iso_i)

foreach var of varlist steu* XP  wav* wgt* {
replace `var'=`var'/OUT_A
}

drop OUT_A

rename XP mxXP

save "$data/exposure_rob", replace























**** comparing the two measures



u $data/XPanama_byHrPRE, clear

merge 1:1 *acid using "$data/port_to_port_sample" 
keep if _merge==3
drop _merge *acid
drop steu*
rename tt tt12
rename hop hop12
rename XP XP12

save help, replace


u $data/XPanama_byHrPRE15, clear

merge 1:1 *acid using "$data/port_to_port_sample_big" 
keep if _merge==3
drop _merge 

merge 1:1 A_port_na D_port_na A_country D_country using help
pwcorr XP12 XPa if _merge==3
br if XP12 != XPa & _merge==3



br if _merge==2



erase help.dta





**************** make another exposure measure that uses the route with largest capacity among the different fastest ones

********* process output from server *******


local files: dir "$sagaoutput" files "*.dta"

foreach file of loc files {

u "$sagaoutput/`file'", clear 

u "$sagaoutput/hrs488_12", clear
	

* keep only the pre period	
keep if A_t<184*24
drop A_time D0_time

g Dpo





